package zz.app.ustbgsmanager.fragments;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.GridLayout;

import java.util.ArrayList;

import zz.app.ustbgsmanager.widgets.IndexButton;
import zz.app.ustbgsmanager.interfaces.OnIndexButtonClickedListener;
import zz.app.ustbgsmanager.R;

/**
 * Created by zz on 2018/6/3 15:16.
 */
public class IndexFragment extends Fragment {
    GridLayout glIcons;
    IndexButton ibQureyScore;
    IndexButton ibQueryCourse;
    IndexButton ibTeachingAssess;
    boolean isShowed = false;
    ArrayList<IndexButton> buttons = new ArrayList<>();
    OnIndexButtonClickedListener listener = null;
    Handler handler;

    public void setListener(OnIndexButtonClickedListener listener) {
        this.listener = listener;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.content_main, container, false);
        buttons.clear();
        ibQureyScore = view.findViewById(R.id.ibQueryScore);
        ibQueryCourse=view.findViewById(R.id.ibQueryCourse);
        ibTeachingAssess=view.findViewById(R.id.ibTeachingAssess);
        buttons.add(ibQureyScore);
        buttons.add(ibQueryCourse);
        if (!isShowed) {
            glIcons = view.findViewById(R.id.glIcons);
            int count = glIcons.getChildCount();
            for (int i = 0; i < count; i++) {
                View v = glIcons.getChildAt(i);
                v.setAlpha(0);
            }
            glIcons.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    iconInAnimation();//加载动画
                    glIcons.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                }
            });
            isShowed = true;
        }


        ibQureyScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllButtonDisabled();
                if (listener != null) {
                    listener.onIndexButtonClicked("QueryScore");
                }
            }
        });
        ibQueryCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllButtonDisabled();
                if (listener != null) {
                    listener.onIndexButtonClicked("QueryCourse");
                }
            }
        });
        ibTeachingAssess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllButtonDisabled();
                if (listener != null) {
                    listener.onIndexButtonClicked("TeachingAssessment");
                }
            }
        });
        return view;
    }

    private void setAllButtonDisabled() {
        for (IndexButton button : buttons) {
            button.setEnabled(false);
        }
    }

    public void setAllButtonEnabled() {
        for (IndexButton button : buttons) {
            button.setEnabled(true);
        }
    }

    private void iconInAnimation() {
        if (handler != null)
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    int count = glIcons.getChildCount();
                    for (int i = 0; i < count; i++) {
                        View v = glIcons.getChildAt(i);
                        final ObjectAnimator animator = ObjectAnimator.ofFloat(v, "alpha", 0, 1);
                        animator.setDuration(500);
                        AnimatorSet set = new AnimatorSet();
                        set.play(animator).after(200 * i);
                        set.start();
                        ScaleAnimation animation = new ScaleAnimation(0.6f, 1, 0.6f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        animation.setDuration(500);
                        animation.setStartOffset(200 * i);
                        v.startAnimation(animation);
                    }
                }
            }, 500);
    }
}
